#!/usr/bin/env bash
set -euo pipefail

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
TMP_DIR="$SCRIPTPATH/../../build"
PLATFORM=$(uname | tr '[:upper:]' '[:lower:]')
HELM_VERSION="3.7.1"
HELM_DIR="${SCRIPTPATH}/../../config/helm"

mkdir -p $TMP_DIR

if [ ! -x "$TMP_DIR/helm" ]; then
    echo "🥑 Downloading the \"helm\" binary"
    curl -L https://get.helm.sh/helm-v$HELM_VERSION-$PLATFORM-amd64.tar.gz | tar zxf - -C $TMP_DIR
    mv $TMP_DIR/$PLATFORM-amd64/helm $TMP_DIR/.
    chmod +x $TMP_DIR/helm
    echo "👍 Downloaded the \"helm\" binary"
fi
export PATH=$TMP_DIR:$PATH

echo "=============================================================================="
echo "                     Linting Helm Chart"
echo "=============================================================================="
helm lint "${HELM_DIR}/aws-node-termination-handler/"

echo "✅ Helm Linting has successfully completed!"

echo "=============================================================================="
echo "             Generate Template from Helm Chart with default values"
echo "=============================================================================="

helm template nth "${HELM_DIR}/aws-node-termination-handler" --namespace=kube-system --debug > /dev/null

echo "=============================================================================="
echo "              Generate Template from Helm Chart with queue-proccessor values"
echo "=============================================================================="

helm template nth "${HELM_DIR}/aws-node-termination-handler" --namespace=kube-system --debug -f "${HELM_DIR}/aws-node-termination-handler/example-values-queue.yaml" > /dev/null

echo "=============================================================================="
echo "              Generate Template from Helm Chart with Linux IMDS values"
echo "=============================================================================="

helm template nth "${HELM_DIR}/aws-node-termination-handler" --namespace=kube-system --debug -f "${HELM_DIR}/aws-node-termination-handler/example-values-imds-linux.yaml" > /dev/null

echo "=============================================================================="
echo "              Generate Template from Helm Chart with Windows IMDS values"
echo "=============================================================================="

helm template nth "${HELM_DIR}/aws-node-termination-handler" --namespace=kube-system --debug -f "${HELM_DIR}/aws-node-termination-handler/example-values-imds-windows.yaml" > /dev/null

echo "✅ Helm template generation has successfully completed!"
